import React, { FC } from 'react'
import { Redirect, Route, RouteProps } from 'react-router-dom'
import { isAuth } from '../../helpers/auth'

export interface PrivateRouteProps extends RouteProps{
  component: React.ComponentType<any>
}

const PrivateRoute: FC<PrivateRouteProps> = ({component: Component, ...rest}) => {
 return (
    <Route
      {...rest}
      render={props=>{
        console.log('PrivateRoute props')
        console.log(props)
        const auth = isAuth()
        if(auth) {
          const {user:{role}} = auth
          if (role === 0) {
            return <Component {...props} />
          }
        }
        return <Redirect to="/signin" />
      }}
      />
  )
}
export default PrivateRoute